namespace SubSonic
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ToolboxData("<{0}:RadioButtons runat=server></{0}:RadioButtons>")]
    public class RadioButtons : RadioButtonList
    {
        private string orderField = string.Empty;
        private string promptText = "None";
        private string promptValue = string.Empty;
        private string providerName = string.Empty;
        private bool showPrompt;
        private string tableName = string.Empty;
        private string textField = string.Empty;
        private string valueField = string.Empty;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if ((!base.DesignMode && (this.Items.Count == 0)) && !string.IsNullOrEmpty(this.tableName))
            {
                Query query = new Query(this.tableName, this.providerName);
                if (string.IsNullOrEmpty(this.valueField) || string.IsNullOrEmpty(this.textField))
                {
                    TableSchema.Table table = DataService.GetSchema(this.tableName, this.providerName, TableType.Table);
                    if (table == null)
                    {
                        throw new Exception("Table name '" + this.tableName + "' using Provider '" + this.providerName + "' doesn't work");
                    }
                    if (string.IsNullOrEmpty(this.valueField))
                    {
                        this.valueField = table.PrimaryKey.ColumnName;
                    }
                    if (string.IsNullOrEmpty(this.textField))
                    {
                        if (table.Columns.Count > 1)
                        {
                            this.textField = table.Columns[1].ColumnName;
                        }
                        else
                        {
                            this.textField = table.Columns[0].ColumnName;
                        }
                    }
                }
                query.SelectList = this.valueField + "," + this.textField;
                if (!string.IsNullOrEmpty(this.OrderField))
                {
                    query.OrderBy = OrderBy.Asc(this.OrderField);
                }
                else
                {
                    query.OrderBy = OrderBy.Asc(this.textField);
                }
                IDataReader reader = null;
                try
                {
                    reader = query.ExecuteReader();
                    while (reader.Read())
                    {
                        ListItem item = new ListItem(reader[1].ToString(), reader[0].ToString());
                        this.Items.Add(item);
                    }
                }
                catch (DataException exception)
                {
                    throw new Exception("Error loading up ListItems for " + this.ClientID + ": " + exception.Message);
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
                ListItem item2 = new ListItem(this.promptText, this.PromptValue);
                if (this.showPrompt)
                {
                    this.Items.Insert(0, item2);
                }
            }
        }

        public string OrderField
        {
            get
            {
                return this.orderField;
            }
            set
            {
                this.orderField = value;
            }
        }

        public string PromptText
        {
            get
            {
                return this.promptText;
            }
            set
            {
                this.promptText = value;
            }
        }

        public string PromptValue
        {
            get
            {
                return this.promptValue;
            }
            set
            {
                this.promptValue = value;
            }
        }

        public string ProviderName
        {
            get
            {
                return this.providerName;
            }
            set
            {
                this.providerName = value;
            }
        }

        public bool ShowPrompt
        {
            get
            {
                return this.showPrompt;
            }
            set
            {
                this.showPrompt = value;
            }
        }

        public string TableName
        {
            get
            {
                return this.tableName;
            }
            set
            {
                this.tableName = value;
            }
        }

        public string TextField
        {
            get
            {
                return this.textField;
            }
            set
            {
                this.textField = value;
            }
        }

        public string ValueField
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }
}

